package com.ruoyi.web.controller.client;

import com.ruoyi.framework.encryption.AesEncryptUtils;
import com.ruoyi.framework.encryption.RSAUtils;
import springfox.documentation.service.ApiListing;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class test {



    public static void main(String[] args) {

        // 查询某年某月的天数
//        System.out.println( " ------ 2020,02 : " + getDayOfMonth(2020,02) );
//        System.out.println( " ------ 2020,03 : " + getDayOfMonth(2020,03) );
//        System.out.println( " ------ 2020,04 : " + getDayOfMonth(2020,04) );
//        System.out.println( " ------ 2060,02 : " + getDayOfMonth(2021,02) );



        // 对方：调用登录接口获取 公钥 、token

        // 对方：随机生成对称加密AES秘钥对医废list数据进行加密，使用公钥对AES随机秘钥进行加密    13006063601
        String yfdata = "[" +
                "{\"codeid\":\"11111111111\",\"stagingId\":\"13000732801\",\"bag\":\"1\",\"collectUserId\":\"890478026930\"," +
                "\"collectUserName\":\"张三三三\",\"collectUpdateDate\":\"2020-12-04 00:08:10\",\"standardDeptCode\":\"A0409\"," +
                "\"sourceDeptId\":\"200814173317832\",\"sourceDeptName\":\"外科\",\"trashType\":\"1\",\"weight\":\"1.5\"," +
                "\"intoUserId\":\"890478026931\",\"intoUserName\":\"李四\",\"intoDate\":\"2020-12-04 00:08:20\",\"outUserId\":\"890478026932\"," +
                "\"outUserName\":\"王五\",\"outDate\":\"2020-12-04 00:08:35\",\"trashStatus\":\"30\"}" +
                "]";
//
//        // 公钥
        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCV+40zZgVVm8e0nrukT4aXmk6X1kQaGxbW73BfBRoYcQQ8CeV69ZnJYVE/MPYCI3TqnYOZAjP2JtALJHMON6LsZ2Eot94PDnVF9dyEdmTJLmuHaU7RER2hGDpnZUizEoUOjmpgHhVxlMQ5BXVAw0rYDYdFyoYeQ1RFUmCgE1wxhQIDAQAB";

        // 公钥加密
        Map<String, String> stringStringMap1 = RSAUtils.publicKeyEncryptString(yfdata, publicKey);
        String encrypted = stringStringMap1.get("encrypted");
        String requestData = stringStringMap1.get("requestData");

        System.out.println( "{\"encrypted\":\"" + encrypted + "\",\"requestData\":\"" + requestData + "\"}" );

        // 对方：调用接口携带token发送加密密匙、加密的字符串

        // 去post模拟，接收结果

        // 对方：公钥解密
        Map<String, String> map = new HashMap<>();
        String encrypted2 = "LL6AnQh+09tpdtqOqa8Y23maoi+5AM/WqPlWUcRwGFOBi6rIDFizMOYplowMvRQcuPPX6KukYMtrJkH2c4shpn2glnM5WEGFvu22XVBZ1lL3jI6j+B73EfF9kYCBQoJkrqDI1BbWxX1LP+gU+V2PKbx6xtIVwxg6xYTK0tUTTOE=";
        String requestData2 = "A+lgMJgBYDYBdtUxAnYL1w84jyOIS77gAITaN9cKGBocInlG3EJMBaLnOakZ/QFDJC1IviXtc7u3YodulBYdHQBGZnMjEPsUd8/CM8MHThd9+YpkTtQc++6UC0thOiq6nW2SaFKFpMEDpcM3jMQE06pGB15vRl8eeSuQnFdG/s6esr05Cfdqg5qJENmcHa6otcSJiHTyceIZpwpnWZA+uUsM4J7UmXd2YCCA6zbdViwZGnoX8uUuCdVWiw90/N7EDomZkKEwy1XNXYk+QpF060WaHwwHAgnK0vjXI3qHzHZz1MVmSMEG9InavrE5l9zNOi4kNY+hQ0v6Ry7EkL8qjKjMZryyY4aypS+WQqyGxGdJdCc3D13A4gDnCKH43Q4tsH5LkOF33lJue3YxG0hHlULLPP0FQrw538zqy8Uj66c=";
        map.put("encrypted",encrypted2 );
        map.put("requestData",requestData2 );
        // 公钥解密

        String AESrandomString = RSAUtils.decryptPublickDataOnJava(encrypted2, publicKey);

        System.out.println( "   解密后秘钥：" + AESrandomString );

        // String encryptedData = RSAUtils.publicKeyDecryptString(map, publicKey);
        // System.out.println( "    解密后结果：" + encryptedData );




//        System.out.println( "   1 " +   checkNameChese("张三李四王五")  );
//        System.out.println( "   2 " +   checkNameChese("鐜嬬淮鍥�")  );
//        System.out.println( "   3 " +   checkNameChese("鍖诲簾璐熻矗浜�")  );
//        System.out.println( "   4 " +   checkNameChese("鑻ヤ緷三")  );
//        System.out.println( "   5 " +   checkNameChese("棰勬 鍒嗚瘖鑴遍槻鎶ょ敤鍝佸")  );
//        System.out.println( "   6 " +   checkNameChese("鎬ヨ瘖绉�") );
//        System.out.println( "   7 " +   checkNameChese("鐪肩")  );
//        System.out.println( "   8 " +   checkNameChese("蹇冪數")  );



    }



    /**
     * 校验一个字符是否是汉字
     *
     * @param c
     *      被校验的字符
     * @return true代表是汉字
     */
    public static boolean isChineseChar(char c) {
        try {
            return String.valueOf(c).getBytes("UTF-8").length > 1;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 验证字符串内容是否包含下列非法字符<br>
     * `~!#%^&*=+\\|{};:'\",<>/?○●★☆☉♀♂※¤╬の〆
     *
     * @param content   字符串内容
     * @return 't'代表不包含非法字符，otherwise代表包含非法字符。
     */
    public static char validateLegalString(String content) {
        String illegal = "`~!#%^&*=+\\|{};:'\",<>/?○●★☆☉♀♂※¤╬の〆";
        char isLegalChar = 't';
        Code1: for (int i = 0; i < content.length(); i++) {//L1定义代码块L1
            for (int j = 0; j < illegal.length(); j++) {
                if (content.charAt(i) == illegal.charAt(j)) {
                    isLegalChar = content.charAt(i);
                    break Code1;
                }
            }
        }
        return isLegalChar;
    }

    /**
     * 验证是否是汉字或者0-9、a-z、A-Z
     *
     * @param c
     *      被验证的char
     * @return true代表符合条件
     */
    public static boolean isRightChar(char c) {
        return isChinese(c) || isWord(c);
    }

    /**
     * 校验某个字符是否是a-z、A-Z、_、0-9
     *
     * @param c
     *      被校验的字符
     * @return true代表符合条件
     */
    public static boolean isWord(char c) {
        String regEx = "[\\w]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher("" + c);
        return m.matches();
    }

    /**
     * 判定输入的是否是汉字
     *
     * @param c
     *      被校验的字符
     * @return true代表是汉字
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    /**
     * 校验String是否全是中文
     *
     * @param name
     *      被校验的字符串
     * @return true代表全是汉字
     */
    public static boolean checkNameChese(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();//转换为数组
        for (int i = 0; i < name.length(); i++) {
            if (!isChinese(cTemp[i])) {//逐个判断是否为中文
                res = false;
                break;
            }
        }
        return res;
    }




    /**
     * 获取某年某月有多少天
     * @param year
     * @param month
     * @return
     */
    public static int getDayOfMonth(int year,int month){
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);
    }


}
